//
//  MyInAndPassViewController.m
//  HuiKaoClient
//
//  Created by 龚 杰 on 13-2-27.
//  Copyright (c) 2013年 龚 杰. All rights reserved.
//

#import "MyInAndPassViewController.h"
#import "MainTabBarController.h"
#import "AppDelegate.h"
@interface MyInAndPassViewController ()

@end

@implementation MyInAndPassViewController
@synthesize My_IP_type = m_My_IP_type;
@synthesize delegate;

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if ([m_titleBttBarItem superview] == nil) {
        [self.navigationController.navigationBar addSubview:m_titleBttBarItem];
    }
    if ([m_BackBttBarItem superview] == nil) {
        [self.navigationController.navigationBar addSubview:m_BackBttBarItem];
    }
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    if ([m_BackBttBarItem superview]) {
        [m_BackBttBarItem removeFromSuperview];
    }
    if ([m_titleBttBarItem superview]) {
        [m_titleBttBarItem removeFromSuperview];
    }
}
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    if (m_My_IP_type == MYIP_IN) {
        m_MyIPDataArray = [[NSMutableArray alloc] initWithObjects:@"邮箱", @"昵称",nil];
        m_titleBttBarItem = [MainTabBarController setTitle:@"个人信息"];
        [m_titleBttBarItem retain];
    }else{
        m_MyIPDataArray = [[NSMutableArray alloc] initWithObjects:@"原密码",@"新密码", @"确认密码",nil];
        m_titleBttBarItem = [MainTabBarController setTitle:@"账号设置"];
        [m_titleBttBarItem retain];
    }
    
    
    m_BackBttBarItem = [UIButton buttonWithType:UIButtonTypeCustom];
    m_BackBttBarItem.frame = CGRectMake(5, 7, 50, 30);
    [m_BackBttBarItem setBackgroundImage:[UIImage imageNamed:@"button_back.png"] forState:UIControlStateNormal];
    [m_BackBttBarItem setTitle:@"返回" forState:UIControlStateNormal];
    [m_BackBttBarItem setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    m_BackBttBarItem.titleLabel.font = [UIFont systemFontOfSize:13.0f];
    [m_BackBttBarItem addTarget:self action:@selector(changeBack) forControlEvents:UIControlEventTouchUpInside];
    [m_BackBttBarItem retain];
    
   
    
    m_IPTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width,self.view.frame.size.height-44) style:UITableViewStyleGrouped];
    UIView* iview = [[UIView alloc] initWithFrame:m_IPTableView.frame];
    [iview setBackgroundColor:[UIColor colorWithRed:242/250.f green:242/255.0f blue:242/255.0f alpha:1.0]];
    m_IPTableView.backgroundView = iview;
    [iview release];
    [m_IPTableView setDataSource:self];
    [m_IPTableView setDelegate:self];
    [self.view addSubview:m_IPTableView];
    [self initFootView];
    
    m_InputField = [[UITextField alloc] initWithFrame:CGRectMake(80, 10, 250, 30)];
    m_InputField1 = [[UITextField alloc] initWithFrame:CGRectMake(80, 10, 250, 30)];
    m_InputField2 = [[UITextField alloc] initWithFrame:CGRectMake(80, 10, 250, 30)];
    
    m_InputField.returnKeyType =UIReturnKeyDone;
    m_InputField1.returnKeyType =UIReturnKeyDone;
    m_InputField2.returnKeyType =UIReturnKeyDone;
    
    m_InputField.delegate = self;
    m_InputField1.delegate = self;
    m_InputField2.delegate = self;
    if (m_My_IP_type == MYIP_IN) {
        NSUserDefaults* tUserDef = [NSUserDefaults standardUserDefaults];
        m_Nickname =  [tUserDef objectForKey:@"username"];
        m_Email = [tUserDef objectForKey:@"email"];
        m_InputField.text = m_Nickname;
        m_InputField1.text = m_Email;
        
    }else{
        
    }

    
}
- (void)initFootView
{
    m_footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320,40)];
    UIButton* ibutton = [UIButton buttonWithType:UIButtonTypeCustom];
    [ibutton setBackgroundImage:[UIImage imageNamed:@"button_yellow_l.png"] forState:UIControlStateNormal];
    ibutton.frame = CGRectMake(8, 0, 304,40);
    [ibutton setTitle:@"提交" forState:UIControlStateNormal];
    [ibutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [ibutton addTarget:self action:@selector(changeToSubmit) forControlEvents:UIControlEventTouchUpInside];
    [m_footView addSubview:ibutton];
    m_IPTableView.tableFooterView = m_footView;
}
- (void)changeBack{
    self.hidesBottomBarWhenPushed = NO;
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)changeToSubmit
{
    [m_InputField resignFirstResponder];
    [m_InputField1 resignFirstResponder];
    [m_InputField2 resignFirstResponder];
    NSUserDefaults* tUserDef = [NSUserDefaults standardUserDefaults];
    NSString* userid = [tUserDef objectForKey:USER_ID];
    NSString* userCode = [tUserDef objectForKey:USER_CODE];
    HttpInterfaceManager* iHttpInterface = [HttpInterfaceManager sharedManager];
    iHttpInterface.delegate = self;
    if (m_My_IP_type == MYIP_IN) {
        NSString* iname = m_InputField.text;
        NSString* iemail = m_InputField1.text;
        if (iname.length>0||iemail.length>0) {
            [iHttpInterface ModifyMyDataRequestGet:userid private_code:userCode username:iname email:iemail old_password:nil password:nil];
        }else{
            AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
            [tAppDel showWithCustomView:@"提示" detailText:@"请输入要修改的信息" isCue:YES delayTime:2.0f];
        }
    }else{
        NSString* iold_password = m_InputField2.text;
        NSString* ipassword = m_InputField.text;
        NSString* ipassword1 = m_InputField1.text;
        if (iold_password.length > 0 && ipassword > 0 && ipassword1 > 0) {
            if ([ipassword isEqual:ipassword1]) {
                [iHttpInterface ModifyMyDataRequestGet:userid private_code:userCode username:nil email:nil old_password:iold_password password:ipassword];
            }else{
                AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
                [tAppDel showWithCustomView:@"提示" detailText:@"两次修改的密码不一致" isCue:YES delayTime:2.0f];
            }
        }else{
            AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
            [tAppDel showWithCustomView:@"提示" detailText:@"请输入正确的密码" isCue:YES delayTime:2.0f];
        }
    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [m_MyIPDataArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* tCellstr = @"EXRCell";
    UITableViewCell* tCell = [tableView dequeueReusableCellWithIdentifier:tCellstr];
    if (tCell == nil) {
        tCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tCellstr] autorelease];
        //        tCell.accessoryType = UITableViewCellAccessoryNone;
        tCell.selectionStyle = UITableViewCellSelectionStyleNone;
        UILabel* iName = [[UILabel alloc] initWithFrame:CGRectMake(5,0,70, 40)];
        iName.tag = 100;
        iName.backgroundColor = [UIColor clearColor];
        [iName setFont:[UIFont systemFontOfSize:16.0f]];
        [iName setTextColor:[UIColor colorWithRed:19.0/255.0f green:19.0/255.0f blue:19.0/255.0f alpha:1.0f]];
        iName.textAlignment = UITextAlignmentCenter;
        [tCell.contentView addSubview:iName];
        [iName release];
        
        if (indexPath.row == 0) {
            [tCell.contentView addSubview:m_InputField];
        }
        else if(indexPath.row == 1){
            [tCell.contentView addSubview:m_InputField1];
        }
        else{
            [tCell.contentView addSubview:m_InputField2];
        }
    }
    
    UILabel* ilabel = (UILabel*)[tCell viewWithTag:100];
    ilabel.text = [m_MyIPDataArray objectAtIndex:indexPath.row];

    return tCell;
}


#pragma mark UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark-
#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

#pragma mark-
#pragma mark HttpInterfaceManageDelegate
-(void)RequestResultsModifyMyData:(NSDictionary*)dic
{
    [self hudWasHidden:nil];
    if (dic != nil) {
        NSInteger result = ((NSString*)[dic objectForKey:@"result"]).integerValue;
        if (result == 1) {
            if (m_My_IP_type == MYIP_IN) {
                if (delegate != nil) {
                    [delegate RefreshMyName:m_InputField2.text];
                }
            }
            AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
            [tAppDel showWithCustomView:@"提示" detailText:@"修改成功" isCue:YES delayTime:2.0f];
            [self performSelector:@selector(changeBack) withObject:nil afterDelay:2.5f];
        }else{
            AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
            [tAppDel showWithCustomView:@"提示" detailText:@"修改失败" isCue:YES delayTime:2.0f];
        }
    }
}
-(void)RequestResultsErrer:(NSDictionary*)dic
{
    [self hudWasHidden:nil];
    AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
    [tAppDel showWithCustomView:@"提示" detailText:[dic objectForKey:@"error_msg"] isCue:YES delayTime:2.0f];
}
#pragma mark-
#pragma mark MBProgressHUDDelegate
- (void)hudWasHidden:(MBProgressHUD *)hud{
    m_MBHUD.delegate = nil;
	[m_MBHUD removeFromSuperview];
	[m_MBHUD release];
	m_MBHUD=nil;
}

-(void) addHudLoad{
	if (m_MBHUD == nil) {
		m_MBHUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
		// Add HUD to screen
        AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
		[tAppDel.window addSubview:m_MBHUD];
		// Regisete for HUD callbacks so we can remove it from the window at the right time
		m_MBHUD.delegate = self;
		m_MBHUD.labelText = @"请求数据中...";
		[m_MBHUD show:YES];
	}
}

- (void)dealloc

{
    [m_IPTableView release];
    [m_titleBttBarItem release];
    [m_BackBttBarItem release];
    [m_MyIPDataArray release];
    [m_footView release];
    [m_InputField resignFirstResponder];
    [m_InputField1 resignFirstResponder];
    [m_InputField2 resignFirstResponder];
    [m_InputField release];
    [m_InputField1 release];
    [m_InputField2 release];
    [super dealloc];
}
@end
